package cn.xuchunh.campusbbs.ui.fragments;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseFragment;
import cn.xuchunh.campusbbs.bean.Reply;
import cn.xuchunh.campusbbs.storage.sqlite.DBManager;
import cn.xuchunh.campusbbs.utils.AppUtil;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;
import cn.xuchunh.campusbbs.web.WebAppInterface;

/**
 * Created by XuChunH on 2015/9/15.
 */
public class TopicReplyDetailFragment extends BaseFragment {

    private Reply reply;
    private int position;

    public static TopicReplyDetailFragment getInstance(int position, Reply reply) {
        TopicReplyDetailFragment topicReplyDetailFragment = new TopicReplyDetailFragment();
        Bundle args = new Bundle();
        args.putSerializable(IntentParamsKey.REPLY, reply);
        args.putInt(IntentParamsKey.POSITION, position);
        topicReplyDetailFragment.setArguments(args);
        return topicReplyDetailFragment;
    }

    public TopicReplyDetailFragment() {
    }

    @Override
    protected void beforeInitView() {
        setContentViewRes(R.layout.fragment_topic_reply_detail);
    }

    @Override
    protected void initView(View view) {
        String positionText = "";
        if (position == 0) {
            positionText = "楼主";
        } else {
            positionText = String.format(getString(R.string.format_topic_reply_position), position + 1);
        }
        ((TextView) view.findViewById(R.id.position)).setText(positionText);
        ((TextView) view.findViewById(R.id.username)).setText(reply.getName());
        ((TextView) view.findViewById(R.id.usertitle)).setText(reply.getUserTitle());
        ((SimpleDraweeView) view.findViewById(R.id.avatar)).setImageURI(Uri.parse(reply.getAvatar()));
        WebView webView = (WebView) view.findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webView.setWebViewClient(webViewClient);
        webView.addJavascriptInterface(new WebAppInterface(getActivity()), "Web");
        String html = AppUtil.getReplyDetailHtmlHeader(getActivity()) + DBManager.getInstance().loadReplyContent(reply.getTopicId(), reply.getId());
        webView.loadData(html, "text/html;charset=UTF-8", null);
    }

    @Override
    public void handleArgs(Bundle args) {
        if (args != null) {
            reply = (Reply) args.getSerializable(IntentParamsKey.REPLY);
            position = args.getInt(IntentParamsKey.POSITION, 0);
        }
    }

    public WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    };
}
